var total = 0;
var page = 1;
var pageSize = 10;
var params = {};
var keyMap = {};
$(function() {
	intSelect();
	init_ershou();
	// pageSearch('/houseapi/listErShouData');
	init_loupan();
	pageLouPanSearch(urlPath+'/houseapi/listLouPanData');
	init_xiaoqu();
	pageXiaoQuSearch(urlPath+'/houseapi/listXiaoQuData');
	init_zufang();
	pageZuFangSearch(urlPath+'/houseapi/listZuFangData');
})

/* function init_ershou() {
	params.page = page;
	params.pageSize = pageSize;
	if ($('#districtSelect').val() == '请选择') {
		params.district = '';
	} else {
		params.district = $('#districtSelect option:selected').text();
	}
	if ($('#areaSelect').val() == '请选择') {
		params.area = '';
	} else {
		params.area = $('#areaSelect option:selected').text();
	}

	$.ajax({
		url: '/houseapi/listErShouData',
		type: 'post',
		async: false,
		data: params,
		success: function(res) {
			if (res.code == 200) {
				var table = "";
				$('#ershoutable').html('');
				total = res.data.total;
				$.each(res.data.rows, function(i, re) {
					table += '<tr>' +
						'<td>' + re.rownum + '</td>' +
						'<td>' + re.city + '</td>' +
						'<td>' + re.district + '</td>' +
						'<td>' + re.area + '</td>' +
						'<td>' + re.total + '</td>' +
						'<td>' + re.buildtime + '</td>' +
						'<td>' + re.layout + '</td>' +
						'<td>' + re.size + '</td>' +
						'<td><button class="btn btn-default" onclick=erShouDetail(' + re.id + ')>详 情</button></td>' +
						'</tr>';
				})
				$('#ershoutable').append(table);
			}
		}
	})
} */

function init_ershou() {
	if ($('#districtSelect').val() == '请选择') {
		keyMap.district = '';
	} else {
		keyMap.district = $('#districtSelect option:selected').text();
	}
	if ($('#areaSelect').val() == '请选择') {
		keyMap.area = '';
	} else {
		keyMap.area = $('#areaSelect option:selected').text();
	}
	$("#ershoutable").bootstrapTable({
		url: urlPath+'/houseapi/listErShouData',
		method: 'post',
		sidePagination: 'server',
		cache: false,
		sortOrder: 'desc',
		contentType: 'application/json',
		pageNumber: 1,
		paginationPreText: "上一页",
		paginationNextText: "下一页",
		rowStyle: function() {
			return {
				css: {
					"text-align": " center"
				}
			}
		},
		queryParams: function(params) {
			params.pageSize = this.pageSize;
			params.page = this.pageNumber;
			params.order = params.order;
			params.keyMap = JSON.stringify(keyMap);
			return params;
		},
		onLoadSuccess: function(data) {
		},
		onLoadError: function(status) {
			alert(status);
		},
		onPageChange: function(number, size) {},
		striped: false,
		pagination: true,
		pageSize: 10,
		pageList: [10, 20, 50],
		minimumCountColumns: 2,
		responseHandler(res) {
			// 插件默认返回
		       return {
		             "rows": res.data.rows, 
		             "total": res.data.total // 数据条数, 用来配置分页
		        };
		    },
		columns: [{
				title: "序号",
				align: "center",
				formatter: function(value, row, index) {
					return index + 1;
				}
			},
			{
				title: "城市",
				align: "center",
				field: "city",
			},
			{
				title: "板块",
				align: "center",
				field: "district",
			},
			{
				title: "区域",
				align: "center",
				field: "area"
			},
			{
				title: "总价",
				align: "center",
				field: "total"
			},
			{
				title: "建成时间",
				align: "center",
				field: "buildtime"
			},
			{
				title: "户型",
				align: "center",
				field: "layout"
			},
			{
				title: "大小",
				align: "center",
				field: "size"
			},
			{
				title: "操作",
				align: "center",
				formatter: function(value, row, index) {
					var html = '<button class="btn btn-default" onclick=erShouDetail(' + row.id + ')>详 情</button>'
					return html;
				}
			}
		],
	});
}



/* function pageSearch(url) {
	var pageNum = 0;
	if (total % 10 == 0) {
		pageNum = total / 10;
	} else {
		pageNum = Math.floor(total / 10) + 1;
	}
	pagination({
		selector: '#page',
		currentPage: 1,
		prev: '上一页',
		next: '下一页',
		first: true,
		last: true,
		showTotalPage: true,
		count: 5, //当前页前后显示的数量
		totalPage: pageNum,
		jumpBtn: true,
		pageOneLoad: false
	}, function(val) {
		var district = '';
		var area = '';
		if ($('#districtSelect').val() == '请选择') {
			district = '';
		} else {
			district = $('#districtSelect option:selected').text();
		}
		if ($('#areaSelect').val() == '请选择') {
			area = '';
		} else {
			area = $('#areaSelect option:selected').text();
		}
		$.ajax({
			url: url,
			type: 'post',
			data: {
				'page': val,
				'pageSize': pageSize,
				'district': district,
				'area': area
			},
			success: function(res) {
				if (res.code == 200) {
					total = res.data.total;
					var table = "";
					$('#ershoutable').html('');
					$.each(res.data.rows, function(i, re) {
						table += '<tr>' +
							'<td>' + re.rownum + '</td>' +
							'<td>' + re.city + '</td>' +
							'<td>' + re.district + '</td>' +
							'<td>' + re.area + '</td>' +
							'<td>' + re.total + '</td>' +
							'<td>' + re.buildtime + '</td>' +
							'<td>' + re.layout + '</td>' +
							'<td>' + re.size + '</td>' +
							'<td><button class="btn btn-default" onclick=erShouDetail(' + re.id + ')>详 情</button></td>' +
							'</tr>';
					})
					$('#ershoutable').append(table);
				}
			}
		})
	})
} */

$('a[data-toggle="tab"]').on('shown.bs.tab', function(e) {
	e.target // newly activated tab
	e.relatedTarget // previous active tab
})

function init_loupan() {
	params.page = page;
	params.pageSize = pageSize;
	$.ajax({
		url: urlPath+'/houseapi/listLouPanData',
		type: 'post',
		async: false,
		data: params,
		success: function(res) {
			if (res.code == 200) {
				var table = "";
				$('#loupantable').html('');
				total = res.data.total;
				$.each(res.data.rows, function(i, re) {
					table += '<tr>' +
						'<td>' + re.rownum + '</td>' +
						'<td>' + re.city + '</td>' +
						'<td>' + re.loupan + '</td>' +
						'<td>' + re.price + '</td>' +
						'<td>' + re.total + '</td>' +
						'</tr>';
				})
				$('#loupantable').append(table);
			}
		}
	})
}

function pageLouPanSearch(url) {
	var pageNum = 0;
	if (total % 10 == 0) {
		pageNum = total / 10;
	} else {
		pageNum = Math.floor(total / 10) + 1;
	}
	pagination({
		selector: '#loupanpage',
		currentPage: 1,
		prev: '上一页',
		next: '下一页',
		first: true,
		last: true,
		showTotalPage: true,
		count: 5, //当前页前后显示的数量
		totalPage: pageNum,
		jumpBtn: true
	}, function(val) {
		$.ajax({
			url: url,
			type: 'post',
			data: {
				'page': val,
				'pageSize': pageSize
			},
			success: function(res) {
				if (res.code == 200) {
					total = res.data.total;
					var table = "";
					$('#loupantable').html('');
					$.each(res.data.rows, function(i, re) {
						table += '<tr>' +
							'<td>' + re.rownum + '</td>' +
							'<td>' + re.city + '</td>' +
							'<td>' + re.loupan + '</td>' +
							'<td>' + re.price + '</td>' +
							'<td>' + re.total + '</td>' +
							'</tr>';
					})
					$('#loupantable').append(table);
				}
			}
		})
	})
}



function init_xiaoqu() {
	params.page = page;
	params.pageSize = pageSize;
	$.ajax({
		url: urlPath+'/houseapi/listXiaoQuData',
		type: 'post',
		async: false,
		data: params,
		success: function(res) {
			if (res.code == 200) {
				var table = "";
				$('#xiaoqutable').html('');
				total = res.data.total;
				$.each(res.data.rows, function(i, re) {
					table += '<tr>' +
						'<td>' + re.rownum + '</td>' +
						'<td>' + re.city + '</td>' +
						'<td>' + re.district + '</td>' +
						'<td>' + re.area + '</td>' +
						'<td>' + re.xiaoqu + '</td>' +
						'<td>' + re.price + '</td>' +
						'<td>' + re.sale + '</td>' +
						'</tr>';
				})
				$('#xiaoqutable').append(table);
			}
		}
	})
}

function pageXiaoQuSearch(url) {
	var pageNum = 0;
	if (total % 10 == 0) {
		pageNum = total / 10;
	} else {
		pageNum = Math.floor(total / 10) + 1;
	}
	pagination({
		selector: '#xiaoqupage',
		currentPage: 1,
		prev: '上一页',
		next: '下一页',
		first: true,
		last: true,
		showTotalPage: true,
		count: 5, //当前页前后显示的数量
		totalPage: pageNum,
		jumpBtn: true
	}, function(val) {
		$.ajax({
			url: url,
			type: 'post',
			data: {
				'page': val,
				'pageSize': pageSize
			},
			success: function(res) {
				if (res.code == 200) {
					total = res.data.total;
					var table = "";
					$('#xiaoqutable').html('');
					$.each(res.data.rows, function(i, re) {
						table += '<tr>' +
							'<td>' + re.rownum + '</td>' +
							'<td>' + re.city + '</td>' +
							'<td>' + re.district + '</td>' +
							'<td>' + re.area + '</td>' +
							'<td>' + re.xiaoqu + '</td>' +
							'<td>' + re.price + '</td>' +
							'<td>' + re.sale + '</td>' +
							'</tr>';
					})
					$('#xiaoqutable').append(table);
				}
			}
		})
	})
}


function init_zufang() {
	params.page = page;
	params.pageSize = pageSize;
	$.ajax({
		url: urlPath+'/houseapi/listZuFangData',
		type: 'post',
		async: false,
		data: params,
		success: function(res) {
			if (res.code == 200) {
				var table = "";
				$('#zufangtable').html('');
				total = res.data.total;
				$.each(res.data.rows, function(i, re) {
					table += '<tr>' +
						'<td>' + re.rownum + '</td>' +
						'<td>' + re.city + '</td>' +
						'<td>' + re.district + '</td>' +
						'<td>' + re.area + '</td>' +
						'<td>' + re.xiaoqu + '</td>' +
						'<td>' + re.type + '</td>' +
						'<td>' + re.layout + '</td>' +
						'<td>' + re.size + '</td>' +
						'<td>' + re.price + '</td>' +
						'</tr>';
				})
				$('#zufangtable').append(table);
			}
		}
	})
}

function pageZuFangSearch(url) {
	var pageNum = 0;
	if (total % 10 == 0) {
		pageNum = total / 10;
	} else {
		pageNum = Math.floor(total / 10) + 1;
	}
	pagination({
		selector: '#zufangpage',
		currentPage: 1,
		prev: '上一页',
		next: '下一页',
		first: true,
		last: true,
		showTotalPage: true,
		count: 5, //当前页前后显示的数量
		totalPage: pageNum,
		jumpBtn: true
	}, function(val) {
		$.ajax({
			url: url,
			type: 'post',
			data: {
				'page': val,
				'pageSize': pageSize
			},
			success: function(res) {
				if (res.code == 200) {
					total = res.data.total;
					var table = "";
					$('#zufangtable').html('');
					$.each(res.data.rows, function(i, re) {
						table += '<tr>' +
							'<td>' + re.rownum + '</td>' +
							'<td>' + re.city + '</td>' +
							'<td>' + re.district + '</td>' +
							'<td>' + re.area + '</td>' +
							'<td>' + re.xiaoqu + '</td>' +
							'<td>' + re.type + '</td>' +
							'<td>' + re.layout + '</td>' +
							'<td>' + re.size + '</td>' +
							'<td>' + re.price + '</td>' +
							'</tr>';
					})
					$('#zufangtable').append(table);
				}
			}
		})
	})
}

function erShouDetail(id) {
	$('#ershouModal').modal('show');
	$.ajax({
		url: urlPath+'/houseapi/getErshouDetails',
		data: {
			'id': id
		},
		type: 'post',
		success: function(res) {
			if (res.code == 200) {
				$('#ershouPic').attr('src', res.data.pic);
				$('#city').text(res.data.city);
				$('#area').text(res.data.district);
				$('#district').text(res.data.area);
				$('#total').text(res.data.total + "万元");
				$('#buildtime').text(res.data.buildtime);
				$('#layout').text(res.data.layout);
				$('#size').text(res.data.size);
				$('#direction').text(res.data.direction);
			}
		}
	})

}

function intSelect() {
	$.ajax({
		url: urlPath+'/houseapi/getDistrict',
		type: 'get',
		success: function(res) {
			if (res.code === 200) {
				var html = $("#districtSelect");
				html.append("<option>请选择</option>");
				for (var i = 0; i < res.data.length; i++) {
					html.append("<option value=" + res.data[i].id + ">" + res.data[i].district + "</option>")
				}
			}
		}
	})
}

$('#districtSelect').change(function() {
	if ($(this).val() != '请选择') {
		$.ajax({
			url: urlPath+'/houseapi/getArea',
			type: 'get',
			data: {
				'pid': $(this).val()
			},
			success: function(res) {
				if (res.code === 200) {
					$('#areaSelect').html('');
					$('#areaSelect').show();
					var html = $("#areaSelect");
					html.append("<option>请选择</option>");
					for (var i = 0; i < res.data.length; i++) {
						html.append("<option value=" + res.data[i].id + ">" + res.data[i].area + "</option>")
					}
				}
			}
		})
	} else {
		$('#areaSelect').hide();
	}
})

function query() {
	if ($('#districtSelect').val() == '请选择') {
		keyMap.district = '';
	} else {
		keyMap.district = $('#districtSelect option:selected').text();
	}
	if ($('#areaSelect').val() == '请选择') {
		keyMap.area = '';
	} else {
		keyMap.area = $('#areaSelect option:selected').text();
	}
	$("#ershoutable").bootstrapTable('refresh',keyMap);
}

function reset() {
	$('#districtSelect').val('请选择');
	$('#districtSelect').trigger('change');
	$('#areaSelect').val('请选择');
}

function retbtn(){
	window.location.href='../index.html'
}
